package org.gitman.agentplatform.ui.summary;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.table.AbstractTableModel;

import org.gitman.agentplatform.agent.api.Agent;
import org.gitman.agentplatform.agent.api.InternalAgentInfo;
import org.gitman.agentplatform.core.AgentManager;
import org.gitman.agentplatform.core.AgentStatistic;
import org.gitman.agentplatform.core.PlatformManager;
import org.gitman.agentplatform.core.Simulation;

public class AgentSummaryTabelModel extends AbstractTableModel {

	private static String [][] columns = { 
		{"ID", "Agent identification number"},
		{"", "Agent color"},
		{"Agent Name", "Agent class name"},
		{"State", "This parameter determinates in what state agent finished simulation"},
		{"Rating", "Rating for each agent is computed as (Pt - Pm)^2"},
		{"Path", "Path traveled (Pt)"},
		{"Min path", "Shortest path (Pm)"},
		{"Failed", "Times agent failed to move due to low reliability"},
		{"Observ", "Observability parameter"},
		{"Predict", "Predictability parameter"},
		{"Relie", "Relieability parameter"},
		{"Time", "Effective time of agent computation (getStep & getPath)"}
	};
	private static int [] widths = {
		40,  // Id
		30,  // color
		260, // name
		80,  // state
		80,  // rating
		80,  // path
		80,  // min path
		80,  // failed
		80,  // observ
		80,  // predict
		80,  // relie
		160  //time		
	};
	
	private PlatformManager platformManager;
	
	public AgentSummaryTabelModel() {
		platformManager = PlatformManager.getInstatnce();
	}
	
	@Override
	public int getRowCount() {
		return platformManager.getSimulation().getAgentManager().getAgents().size();
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Simulation simulation = platformManager.getSimulation();
		AgentManager agentManager = simulation.getAgentManager();
		ArrayList<Agent> list =  new ArrayList<Agent>(agentManager.getAgents());
		Collections.sort(list);
		Agent agent = list.get(rowIndex);
		InternalAgentInfo info = agentManager.getAgentInfo(agent);
		AgentStatistic statistic = simulation.getAgentStatistic(info.getId());
		
		switch (columnIndex) {
		case 0:
			return info.getId();
		case 1:
			return info.getColor();
		case 2:
			return agent.getClass().getSimpleName();
		case 3:
			return info.getAgentState();
		case 4: 
			return statistic.getRating();
		case 5:
			return statistic.getPathSize();
		case 6:
			return statistic.getMinPathSize();
		case 7:
			return statistic.getFailed();
		case 8:
			return info.getObservString();
		case 9:
			return info.getPredictString();
		case 10:
			return info.getRelieString();
		case 11:
			return statistic.getTime();
		default:
			return "###";
		}		
	}

	
	public String getColumnDesciption(int column) {
		return columns[column][1];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Class<?> getColumnClass(int column) {
		if(column == 1){
			return Color.class;
		}else{
			return String.class;
		}
	}

	@Override
	public String getColumnName(int column) {
		return columns[column][0];
	}

	
	public int[] getWidths() {
		return widths;
	}
}
